package com.tinghua.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;


@Slf4j
@RestController
@RequestMapping("/queue")
public class QueueController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendTTLRestrict/{message}")
    public void sendQAAndQB(@PathVariable("message") String message){
        log.info("在{}ms时刻触发定向时间请求:{}", new Date(), message);
        rabbitTemplate.convertAndSend("EA", "EA", "消息来自10ms延迟的队列" + message);
        rabbitTemplate.convertAndSend("EA", "EB", "消息来自40ms延迟的队列" + message);
    }

    @GetMapping("/sendTTLNonRestrict/{message}/{time}")
    public void sendQC(@PathVariable("message") String message, @PathVariable("time") String time){
        log.info("在{}ms时刻触发自主选择{}时间请求内容:{}", new Date(), time, message);
        rabbitTemplate.convertAndSend("EA", "EA", "消息来自" + time + "ms延迟的队列" + message, msg -> {
            msg.getMessageProperties().setExpiration(time);
            return msg;
        });

    }
}
